<template>
  <div class="app-container">
    <div class="filter-container">
      <span style="float:left;margin-left: 10px;">
        <el-button @click="handleFilter" :disabled="btnDisabled" class="filter-item" type="primary" v-waves icon="el-icon-search">刷新</el-button>
        <el-button @click="handleCreate" v-if="sysRole_btn_add" :disabled="btnDisabled" class="filter-item"
          type="primary" v-waves icon="el-icon-plus">添加
        </el-button>
      </span>
    </div>
    <!-- 表格 -->
    <el-table :data="list" v-loading="listLoading" border fit highlight-current-row style="width: 100%;height: 100%;">
      <el-table-column width="60px" align="center" label="序号">
        <template slot-scope="scope">
          <span>{{ scope.$index +1 + (listQuery.pageSize*(listQuery.page-1))}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="code" label="角色编码" width="250px" align="center" />
      <el-table-column prop="name" label="角色名称" width="250px" align="center" />
      <el-table-column label="状态" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{getNameByCode(scope.row.status, allDictData.statusOps)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="memo" label="描述" width="300px" align="center" />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button v-if="sysRole_btn_relate" @click="handleRelateUsers(scope.row.id)" size="small" type="info"
            v-waves>关联用户</el-button>
          <el-button v-if="sysRole_btn_authorize" @click="handleAuthorize(scope.row.id)" size="small" type="primary"
            v-waves>授权</el-button>
          <el-button v-if="sysRole_btn_edit" @click="handleUpdate(scope.row.id)" size="small" type="success" v-waves
            icon="el-icon-edit">编辑</el-button>
          <el-button v-if="sysRole_btn_del" @click="handleDelete(scope.row.id)" size="small" type="danger" v-waves
            icon="el-icon-delete">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>


    <!-- 关联用户页面组件 -->
    <user v-if="showRelateUser" @flushList="flushList" @closeDialog="closePage" :showRelateUser="showRelateUser"
      :roleId="rowId" />

    <!-- 关联用户页面组件 -->
    <authorize v-if="showAuthorize" @flushList="flushList" @closeDialog="closePage" :restPath="restPath"
      :showAuthorize="showAuthorize" :roleId="rowId" />

    <!-- 添加修改弹出框页面组件 -->
    <add-update v-if="showAdd" @flushList="flushList" @closeDialog="closeDialog" :restPath="restPath" :showAdd="showAdd"
      :btnType="btnType" :rowId="rowId" :allDictData="allDictData" />

  </div>
</template>

<script>
  /* mixins */
  import pageListMixin from '@/mixins/pageList/pageListMixin.js'

  import {
    getDictData
  } from '@/api/common'

  /* 组件 */
  import user from './components/user/index.vue'
  import authorize from './components/authorize/index.vue'
  import addUpdate from './components/addUpdate.vue'

  import * as common from '@/utils/base/common'
  import {
    getNameByCode
  } from '@/utils/dictData'

  export default {
    /*  内容: 角色  */
    name: 'role',
    data() {
      return {
        restPath: 'sys/role',
        listQuery: {
          page: 1,
          pageSize: 10,
        },
        sysRole_btn_relate: true,
        sysRole_btn_authorize: true,
        sysRole_btn_edit: true,
        sysRole_btn_del: true,
        sysRole_btn_add: true,

        allDictData: {
          statusOps: []
        },

        showRelateUser: false,
        showAuthorize: false,

      }
    },
    mixins: [pageListMixin],
    components: {
      'add-update': addUpdate,
      user,
      authorize,
    },
    methods: {
      initData() {
        //              this.sysRole_btn_relate = this.elements['sysRole:btn_relate']
        //              this.sysRole_btn_authorize = this.elements['sysRole:btn_authorize']
        //              this.sysRole_btn_edit = this.elements['sysRole:btn_edit']
        //              this.sysRole_btn_del = this.elements['sysRole:btn_del']
        //              this.sysRole_btn_add = this.elements['sysRole:btn_add']
        this.getAllDictData()
        this.getList()
      },

      getAllDictData() {
        const params = {
          dictIds: "STATUS"
        }
        getDictData(params).then(response => {
          let data = response.data
          this.allDictData.statusOps = data.STATUS
        }).catch(err => {
          common.message(this, 'error', 2, '获取数据字典失败!')
          console.error(err)
        })
      },

      getNameByCode(val, dictMap) {
        return getNameByCode(val, dictMap)
      },

      handleRelateUsers(rowId) {
        this.showRelateUser = true
        this.rowId = rowId;
      },

      handleAuthorize(rowId) {
        this.showAuthorize = true
        this.rowId = rowId;
      },

      closePage() {
        this.showRelateUser = false
        this.showAuthorize = false
      },

    } // methods end
  }
</script>
<style scoped>
  @import "~@/styles/list.css";
</style>
